/*
 * InterlockedCompareExchange.cpp
 *
 *  Created on: 2011/11/29
 *      Author: naoki
 */

namespace NTEngine {

// Win32のInterlockedCompareExchange互換
int InterlockedCompareExchange( volatile int* dest, int exch, int comp ) {
	int old, tmp;
	__asm__ __volatile__ (
		"L_%=:ldrex %1, [%2]\n\t"
		"teq %1, %3\n\t"
		"strexeq %0, %4, [%2]\n\t"
		"cmp %0, #0\n\t"
		"bne L_%=\n\t"
		: "=&r" (tmp), "=&r" (old)
		: "r" (dest), "r" (comp), "r" (exch)
		: "memory" );
	return old;
}

} // namespace NTEngine
